//--------------------------------------- NOTES ------------------------------------------------------//
* This table is Table 6 in the paper
//--------------------------------------------------------------------------------------------------------//

cd  "/YOUR_LOCAL_DIRECTORY" //setting the working directory
clear all //remove all data, labels, matrices etc (incl. Mata functions)
use December8ChanRoth, clear

** Turn flat file into player flat file
keep if round==10
expand 2, gen(dupindicator)
gen playerid = player1name if dupindicator==0
replace playerid = player2name if dupindicator==1

** Make identifiers for data-linking
encode playerid, gen(id)

* Create the key treatment variable
gen byte status_quo = treatment=="status_quo"

* Merge files
merge m:1 id using Qualtrics_data.dta
drop _merge==3

* Label variables
la var player1bluecount  "# Blue Balls in Jars"
gen player2bluecount = (player2highbluecount+player2lowbluecount)/2
la var player2bluecount "# Blue Balls in Urns"
gen earnings = player1earnings if dupindicator==0
replace earnings = player2earnings if dupindicator==1
la var earnings "Player Earnings (in $0.01)"

* balance table variables

global DESCVARS female white black asian otherrace hispanic employed unemployed collegegrad HSgrad age18to24 age25to34 age35to44 age45to54 age55to64 age65to74 age75to84 player1bluecount player2bluecount 
mata: mata clear


* First test of differences
local i = 1

foreach var in $DESCVARS {
    reg `var' status_quo if `var'==player1bluecount & dupindicator==0 | `var'==player2bluecount & dupindicator==1 | `var'!=player1bluecount & `var'!=player2bluecount
    outreg, keep(status_quo)  rtitle("`: var label `var''") stats(p) ///
        noautosumm store(row`i')  starlevels(10 5 1) starloc(1)
    outreg, replay(diff) append(row`i') ctitles("",Difference ) ///
        store(diff) note("")
    local ++i
}
outreg, replay(diff)



* Then Summary statistics
local count: word count $DESCVARS
mat sumstat = J(`count',6,.)

local i = 1
foreach var in $DESCVARS {
    quietly: summarize `var' 
    mat sumstat[`i',1] = r(mean)
    mat sumstat[`i',2] = r(sd)
    quietly: summarize `var' if status_quo==0
    mat sumstat[`i',3] = r(mean)
    mat sumstat[`i',4] = r(sd)
	quietly: summarize `var' if status_quo==1
    mat sumstat[`i',5] = r(mean)
    mat sumstat[`i',6] = r(sd)
    local i = `i' + 1
}
frmttable, statmat(sumstat) store(sumstat) sfmt(g,f,f,g,f,f)

outreg using "/Users/alexchan/Desktop/Chan-Roth/balancetable_alt", ///
    replay(sumstat) merge(diff) tex nocenter note("") fragment plain replace ///
    ctitles("", Total, "", Holistic, "", Status Quo, "", "" \ "", mean, sd, mean, sd, mean, sd, Difference p-value) ///
    multicol(1,2,3;1,5,3) 
